home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Misc / bgui / Examples / Source / ColorWheel.c < prev    next >
C/C++ Source or Header  |  2000-05-09  |  28KB  |  400 lines

  1. /*
  2.  * @(#) $Header: /cvsroot/bgui/examples/ColorWheel.c,v 41.11 2000/05/09 20:33:22 mlemos Exp $
  3.  *
  4.  * ColorWheel.c
  5.  *
  6.  * (C) Copyright 1998 Manuel Lemos.
  7.  * (C) Copyright 1995 Jaba Development.
  8.  * (C) Copyright 1995 Jan van den Baard.
  9.  * All Rights Reserved.
  10.  *
  11.  * $Log: ColorWheel.c,v $
  12.  * Revision 41.11  2000/05/09 20:33:22  mlemos
  13.  * Bumped to revision 41.11
  14.  *
  15.  * Revision 1.2  2000/05/09 19:58:42  mlemos
  16.  * Merged with the branch Manuel_Lemos_fixes.
  17.  *
  18.  * Revision 1.1.2.1  1998/02/28 17:45:05  mlemos
  19.  * Ian sources
  20.  *
  21.  *
  22.  */
  23.  
  24. /* Execute me to compile with DICE V3.0
  25. dcc ColorWheel.c -proto -mi -ms -mRR -3.0 -lbgui
  26. quit
  27. */
  28.  
  29. /*
  30.  *      This example is based on the original ColorWheel.c
  31.  *      code by Commodore.
  32.  */
  33.  
  34. #include "DemoCode.h"
  35.  
  36. #include <dos/dos.h>
  37. #include <gadgets/colorwheel.h>
  38. #include <gadgets/gradientslider.h>
  39. #include <graphics/displayinfo.h>
  40.  
  41. #include <clib/colorwheel_protos.h>
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45.  
  46. /*
  47. **      Library base pointers.
  48. **/
  49. struct Library *ColorWheelBase = NULL, *GradientSliderBase = NULL;
  50.  
  51. /*
  52. **      Object ID's
  53. **/
  54. #define ID_QUIT                 1
  55. #define ID_WHEEL                2
  56.  
  57. /*
  58. **      Structure for LoadRGB32().
  59. **/
  60. struct load32 {
  61.         UWORD           l32_len;
  62.         UWORD           l32_pen;
  63.         ULONG           l32_red;
  64.         ULONG           l32_grn;
  65.         ULONG           l32_blu;
  66. };
  67.  
  68. /*
  69. **      Ensure correct operation on ECS.
  70. **/
  71. #define GRADCOLORS 4
  72.  
  73. /*
  74. **      The program even does version checking... sjeez...
  75. **/
  76. extern struct Library *SysBase;
  77.  
  78. /*
  79. **      And were in...
  80. **/
  81. VOID StartDemo( void )
  82. {
  83.         Object                  *WN_Window, *GA_Quit, *GA_Wheel, *GA_GrSlider, *GA_Master, *RealSlider = NULL;
  84.         struct Window           *win;
  85.         struct Screen           *scr;
  86.         ULONG                   winsig = 0L, sigrec, rc;
  87.         BOOL                    running = TRUE;
  88.         ULONG                   colortable[ 96 ];
  89.         struct load32           color_list[ GRADCOLORS + 1 ];
  90.         WORD                    penns[ GRADCOLORS + 1 ];
  91.         struct ColorWheelRGB    rgb;
  92.         struct ColorWheelHSB    hsb;
  93.         ULONG                   modeID = HIRES_KEY;
  94.         UWORD                   numPens, i;
  95.         DisplayInfoHandle       displayhandle;
  96.         struct DimensionInfo    dimensioninfo;
  97.  
  98.         /*
  99.         **      Need OS 3.0.
  100.         **/
  101.         if ( SysBase->lib_Version < 39 ) {
  102.                 Tell( "OS 3.0 required!" );
  103.                 exit( 0 );
  104.         }
  105.  
  106.         /*
  107.         **      Open the libraries. Get display information etc...
  108.         **/
  109.         if ( ColorWheelBase = OpenLibrary( "gadgets/colorwheel.gadget", 39L )) {
  110.                 if ( GradientSliderBase = OpenLibrary( "gadgets/gradientslider.gadget", 39L )) {
  111.                         if ( displayhandle = FindDisplayInfo( modeID )) {
  112.                                 if ( GetDisplayInfoData( displayhandle, ( UBYTE * )&dimensioninfo, sizeof( struct DimensionInfo ), DTAG_DIMS, NULL )) {
  113.                                         /*
  114.                                         **      Open up a screen with as many
  115.                                         **      colors as possible. Also make it
  116.                                         **      like your workbench screen.
  117.                                         **/
  118.                                         if ( scr = OpenScreenTags( NULL, SA_Depth,              dimensioninfo.MaxDepth,
  119.                                                                          SA_SharePens,          TRUE,
  120.                                                                          SA_LikeWorkbench,      TRUE,
  121.                                                                          SA_Interleaved,        TRUE,
  122.                                                                          SA_Title,              "ColorWheel Screen",
  123.                                                                          TAG_END )) {
  124.                                                 /*
  125.                                                 **      Get the colors.
  126.                                                 **/
  127.                                                 GetRGB32( scr->ViewPort.ColorMap, 0L, 32L, colortable );
  128.  
  129.                                                 /*
  130.                                                 **      Setup gradient slider
  131.                                                 **      as color 0.
  132.                                                 **/
  133.                                                 rgb.cw_Red   = colortable[ 0 ];
  134.                                                 rgb.cw_Green = colortable[ 1 ];
  135.                                                 rgb.cw_Blue  = colortable[ 2 ];
  136.  
  137.                                                 /*
  138.                                                 **      Convert the RGB values to
  139.                                                 **      HSB values.
  140.                                                 **/
  141.                                                 ConvertRGBToHSB( &rgb, &hsb );
  142.  
  143.                                                 /*
  144.                                                 **      Maximum brightness.
  145.                                                 **/
  146.                                                 hsb.cw_Brightness = 0xffffffff;
  147.  
  148.                                                 numPens = 0;
  149.  
  150.                                                 /*
  151.                                                 **      Build the pen array for the
  152.                                                 **      gradient slider (4 pens).
  153.                                                 **/
  154.                                                 while ( numPens < GRADCOLORS ) {
  155.                                                         /*
  156.                                                         **      Compute dim level of this pen.
  157.                                                         **/
  158.                                                         hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / GRADCOLORS ) * numPens );
  159.                                                         /*
  160.                                                         **      Convert this dim level
  161.                                                         **      to RGB values.
  162.                                                         **/
  163.                                                         ConvertHSBToRGB( &hsb, &rgb );
  164.                                                         /*
  165.                                                         **      Allocate a pen.
  166.                                                         **/
  167.                                                         if (( penns[ numPens ] = ObtainPen( scr->ViewPort.ColorMap, -1, rgb.cw_Red, rgb.cw_Green, rgb.cw_Blue, PEN_EXCLUSIVE )) == -1 )
  168.                                                                 break;
  169.                                                         /*
  170.                                                         **      Stuff it in the color list.
  171.                                                         **/
  172.                                                         color_list[ numPens ].l32_len = 1;
  173.                                                         color_list[ numPens ].l32_pen = penns[ numPens ];
  174.                                                         /*
  175.                                                         **      Next...
  176.                                                         **/
  177.                                                         numPens++;
  178.                                                 }
  179.  
  180.                                                 /*
  181.                                                 **      Terminate pen array and
  182.                                                 **      color list.
  183.                                                 **/
  184.                                                 penns[ numPens ]              = ~0;
  185.                                                 color_list[ numPens ].l32_len = 0;
  186.  
  187.                                                 /*
  188.                                                 **      Create the gradient slider here
  189.                                                 **      _with_ the EXT_NoRebuild tag set
  190.                                                 **      to TRUE. This _must_ be set to
  191.                                                 **      TRUE otherwise the external class
  192.                                                 **      will rebuild the object after each
  193.                                                 **      re-size resulting in colourful
  194.                                                 **      crashes when it gets attached to
  195.                                                 **      the colorwheel :)
  196.                                                 **/
  197.                                                 GA_GrSlider = ExternalObject,
  198.                                                         EXT_MinWidth,           10,
  199.                                                         EXT_MinHeight,          10,
  200.                                                         EXT_ClassID,            "gradientslider.gadget",
  201.                                                         EXT_NoRebuild,          TRUE,
  202.                                                         GRAD_PenArray,          penns,
  203.                                                         PGA_Freedom,            LORIENT_VERT,
  204.                                                         TAG_END );
  205.  
  206.                                                 /*
  207.                                                 **      Pick up a pointer to the
  208.                                                 **      "real" gradient slider object.
  209.                                                 **      This will be the pointer we
  210.                                                 **      pass to the colorwheel.
  211.                                                 **/
  212.                                                 if ( GA_GrSlider )
  213.                                                         GetAttr( EXT_Object, GA_GrSlider, ( ULONG * )&RealSlider );
  214.  
  215.                                                 /*
  216.                                                 **      Create a small window.
  217.                                                 **/
  218.                                                 WN_Window = WindowObject,
  219.                                                         WINDOW_Title,           "ColorWheel",
  220.                                                         WINDOW_RMBTrap,         TRUE,
  221.                                                         WINDOW_Screen,          scr,
  222.                                                         WINDOW_ScaleWidth,      20,
  223.                                                         WINDOW_ScaleHeight,     20,
  224.                                                         WINDOW_AutoAspect,      TRUE,
  225.                                                         /*
  226.                                                         **      The colorwheel is really slow :(
  227.                                                         **/
  228.                                                         WINDOW_NoBufferRP,      TRUE,
  229.                                                         WINDOW_MasterGroup,
  230.                                                                 GA_Master = VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
  231.                                                                         StartMember, TitleSeperator( "Wheel & Slider" ), EndMember,
  232.                                                                         StartMember,
  233.                                                                                 HGroupObject, Spacing( 4 ),
  234.                                                                                         StartMember,
  235.                                                                                                 /*
  236.                                                                                                 **      The EXT_NoRebuild tag may _not_ be set to
  237.                                                                                                 **      TRUE for a colorwheel object. This is due
  238.                                                                                                 **      to the fact that colorwheels cannot change
  239.                                                                                                 **      size. This is also the reason why we need
  240.                                                                                                 **      to track the attributes of the colorwheel.
  241.                                                                                                 **/
  242.                                                                                                 GA_Wheel = ExternalObject,
  243.                                                                                                         EXT_MinWidth,           30,
  244.                                                                                                         EXT_MinHeight,          30,
  245.                                                                                                         EXT_ClassID,            "colorwheel.gadget",
  246.                                                                                                         WHEEL_Screen,           scr,
  247.                                                                                                         /*
  248.                                                                                                         **      Pass a pointer to the "real" gradient slider
  249.                                                                                                         **      here.
  250.                                                                                                         **/
  251.                                                                                                         WHEEL_GradientSlider,   RealSlider,
  252.                                                                                                         WHEEL_Red,              colortable[ 0 ],
  253.                                                                                                         WHEEL_Green,            colortable[ 1 ],
  254.                                                                                                         WHEEL_Blue,             colortable[ 2 ],
  255.                                                                                                         GA_FollowMouse,         TRUE,
  256.                                                                                                         GA_ID,                  ID_WHEEL,
  257.                                                                                                         /*
  258.                                                                                                         **      These attributes of the colorwheel are
  259.                                                                                                         **      tracked and reset to the object after
  260.                                                                                                         **      it has been rebuild. This way the current
  261.                                                                                                         **      colorwheel internals will not be lost
  262.                                                                                                         **      after the object is re-build.
  263.                                                                                                         **/
  264.                                                                                                         EXT_TrackAttr,          WHEEL_Red,
  265.                                                                                                         EXT_TrackAttr,          WHEEL_Green,
  266.                                                                                                         EXT_TrackAttr,          WHEEL_Blue,
  267.                                                                                                         EXT_TrackAttr,          WHEEL_Hue,
  268.                                                                                                         EXT_TrackAttr,          WHEEL_Saturation,
  269.                                                                                                         EXT_TrackAttr,          WHEEL_Brightness,
  270.                                                                                                 EndObject,
  271.                                                                                         EndMember,
  272.                                                                                         /*
  273.                                                                                         **      Add the externalclass object of the
  274.                                                                                         **      gradient slider here. Right next to
  275.                                                                                         **      the colorwheel :)
  276.                                                                                         **/
  277.                                                                                         StartMember,
  278.                                                                                                 GA_GrSlider, FixWidth( 20 ),
  279.                                                                                         EndMember,
  280.                                                                                 EndObject,
  281.                                                                         EndMember,
  282.                                                                         StartMember, HorizSeperator, EndMember,
  283.                                                                         StartMember,
  284.                                                                                 HGroupObject,
  285.                                                                                         VarSpace( DEFAULT_WEIGHT ),
  286.                                                                                         StartMember, GA_Quit = KeyButton( "_Quit", ID_QUIT ), EndMember,
  287.                                                                                         VarSpace( DEFAULT_WEIGHT ),
  288.                                                                                 EndObject, FixMinHeight,
  289.                                                                         EndMember,
  290.                                                                 EndObject,
  291.                                                 EndObject;
  292.  
  293.                                                 if ( WN_Window ) {
  294.                                                         /*
  295.                                                         **      Make button selectable by the keyboard.
  296.                                                         **/
  297.                                                         GadgetKey( WN_Window, GA_Quit, "q" );
  298.                                                         /*
  299.                                                         **      Open up the window.
  300.                                                         **/
  301.                                                         if ( win = WindowOpen( WN_Window )) {
  302.                                                                 /*
  303.                                                                 **      Obtain window sigmask.
  304.                                                                 **/
  305.                                                                 GetAttr( WINDOW_SigMask, WN_Window, &winsig );
  306.                                                                 /*
  307.                                                                 **      Wait for messages.
  308.                                                                 **/
  309.                                                                 do {
  310.                                                                         sigrec = Wait( winsig );
  311.  
  312.                                                                         /*
  313.                                                                         **      Window signal?
  314.                                                                         **/
  315.                                                                         if ( sigrec & winsig ) {
  316.                                                                                 while ( WN_Window && (( rc = HandleEvent( WN_Window )) != WMHI_NOMORE )) {
  317.                                                                                         switch ( rc ) {
  318.  
  319.                                                                                                 case    WMHI_CLOSEWINDOW:
  320.                                                                                                 case    ID_QUIT:
  321.                                                                                                         /*
  322.                                                                                                         **      The end.
  323.                                                                                                         **/
  324.                                                                                                         running = FALSE;
  325.                                                                                                         break;
  326.  
  327.                                                                                                 case    ID_WHEEL:
  328.                                                                                                         /*
  329.                                                                                                         **      Obtain the wheel it's HSB settings.
  330.                                                                                                         **/
  331.                                                                                                         GetAttr( WHEEL_HSB, GA_Wheel, ( ULONG * )&hsb );
  332.  
  333.                                                                                                         i = 0;
  334.  
  335.                                                                                                         /*
  336.                                                                                                         **      Recompute the gradient slider
  337.                                                                                                         **      dim colors for the current
  338.                                                                                                         **      colorwheel setting.
  339.                                                                                                         **/
  340.                                                                                                         while ( i < numPens ) {
  341.                                                                                                                 /*
  342.                                                                                                                 **      Compute dim level of this pen.
  343.                                                                                                                 **/
  344.                                                                                                                 hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / numPens ) * i );
  345.                                                                                                                 /*
  346.                                                                                                                 **      Convert to RGB values.
  347.                                                                                                                 **/
  348.                                                                                                                 ConvertHSBToRGB(&hsb,&rgb);
  349.                                                                                                                 /*
  350.                                                                                                                 **      Setup the pens in the color list.
  351.                                                                                                                 **/
  352.                                                                                                                 color_list[ i ].l32_red = rgb.cw_Red;
  353.                                                                                                                 color_list[ i ].l32_grn = rgb.cw_Green;
  354.                                                                                                                 color_list[ i ].l32_blu = rgb.cw_Blue;
  355.                                                                                                                 /*
  356.                                                                                                                 **      Next...
  357.                                                                                                                 **/
  358.                                                                                                                 i++;
  359.                                                                                                         }
  360.                                                                                                         /*
  361.                                                                                                         **      Show the changes in the gradient slider.
  362.                                                                                                         **/
  363.                                                                                                         LoadRGB32( &scr->ViewPort, ( ULONG * )color_list );
  364.                                                                                                         break;
  365.                                                                                         }
  366.                                                                                 }
  367.                                                                         }
  368.                                                                 } while ( running );
  369.                                                         }
  370.                                                         /*
  371.                                                         **      Kill the window.
  372.                                                         **/
  373.                                                         DisposeObject( WN_Window );
  374.                                                 }
  375.                                                 /*
  376.                                                 **      Release the allocated pens.
  377.                                                 **/
  378.                                                 while ( numPens > 0 ) {
  379.                                                         numPens--;
  380.                                                         ReleasePen( scr->ViewPort.ColorMap, penns[ numPens ] );
  381.                                                 }
  382.                                                 /*
  383.                                                 **      Close the screen.
  384.                                                 **/
  385.                                                 CloseScreen( scr );
  386.                                         }
  387.                                 }
  388.                         }
  389.                         /*
  390.                         **      Close the gradient.gadget.
  391.                         **/
  392.                         CloseLibrary( GradientSliderBase );
  393.                 }
  394.                 /*
  395.                 **      Close the colorwheel.gadget.
  396.                 **/
  397.                 CloseLibrary( ColorWheelBase );
  398.         }
  399. }
  400.